const knex = require("../../db/knex");
const jwt = require('jsonwebtoken')

async function login(param, res) {
    let {username, pwd} = param
    console.log(param.username, '双大蛇')
    await knex.transaction(async trx => {
        let response = await trx('userlist').select('*').where({userId: username}).andWhere({pwd: pwd})

        if (response.length > 0) {
            let roleInfo = await trx('roles').select('*').where({role_description: response[0].role})
            //存在token的数据
            console.log(roleInfo[0], '签到')
            const paylod = {userId: response[0].userId, role: response[0].role, role_id: roleInfo[0].role_id}
            //secret
            const secret = 'mysecret'
            //生成token
            const token = jwt.sign(paylod, secret, {expiresIn: "1h"})
            res.status(200).send({
                info: response[0],
                token
            })
        } else {
            res.status(210).send({
                msg: "失败"
            })
        }
    })
}

module.exports = login
